package com.web.my12306.service.impl;

import com.web.my12306.dao.IOrderDao;
import com.web.my12306.dao.ISeatDao;
import com.web.my12306.dao.ITrainDao;
import com.web.my12306.dao.impl.OrderDaoImpl;
import com.web.my12306.dao.impl.SeatDaoImpl;
import com.web.my12306.dao.impl.TrainDaoImpl;
import com.web.my12306.pojo.Seat;
import com.web.my12306.pojo.Train;
import com.web.my12306.service.TrainService;

import java.sql.Timestamp;
import java.util.List;

public class TrainServiceImpl implements TrainService
{
    ITrainDao trainDao = new TrainDaoImpl();
    ISeatDao seatDao = new SeatDaoImpl();
    IOrderDao orderDao=new OrderDaoImpl();

    @Override
    public void add(Train train,int carriage)
    {

        int tid=0;
        if(trainDao.add(train))
        {
            tid = trainDao.queryByTrainno(train.getTrainno(), Timestamp.valueOf(train.getDeparture_Time().toString().substring(0, 10) + " 00:00:00"));
        }//查出新加入车次id，保留
        int row = 15;//每节车厢座位排数
        String[] seatno = {"A", "B", "C", "D", "F"};
        Seat seat;
        for (int i = 0; i < carriage; i++)//遍历车厢号
        {
            for (int j = 0; j < row; j++)//遍历每个车厢排数
            {
                for (String s : seatno)//遍历每排编号
                {
                    seat = new Seat();
                    seat.setTid(tid);
                    seat.setCarriage(i + 1);
                    seat.setSeatno((j + 1) + s);
                    seat.setState(1);
                    seatDao.add(seat);
                }

            }
        }

    }
    public void cancelByTid(int tid)
    {
         trainDao.editByTid(tid,4);
         orderDao.editState(tid,3);
    }
    
    /**
     * 实现查询所有车次信息
     */
	@Override
	public List<Train> getTrainAll() {
		return trainDao.getTrainAll();
	}
	/**
	 * 实现按多條件查询所有车次信息
	 */
	
	@Override
	public List<Train> getTrainBySelect(String dstation,String terminal) {
		
		return trainDao.getTrainBySelect( dstation,terminal);
	}


}
